home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / progwin.zip / ENVIRON.ZIP / ENVIRON.C < prev    next >
Text File  |  1991-10-03  |  3KB  |  125 lines

  1.  /*-------------------------------------
  2.   ENVIRON.C --Environment List Box
  3.          (c) Charles Petzold, 1990
  4.  ---------------------------------------*/
  5.  #include <windows.h>
  6.  #include <stdlib.h>
  7.  #include <string.h>
  8.  #define MAXENV 4096
  9.  
  10.  long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  11.  
  12.  int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  13.               LPSTR lpszCmdLine, int nCmdShow)
  14.        {
  15.        static char szAppName[] = "Environ" ;
  16.        HWND        hwnd;
  17.        MSG       msg;
  18.        WNDCLASS       wndclass;
  19.  
  20.        if (!hPrevInstance)
  21.         {
  22.         wndclass.style     = CS_HREDRAW | CS_VREDRAW;
  23.         wndclass.lpfnWndProc = WndProc;
  24.         wndclass.cbClsExtra     = 0;
  25.         wndclass.cbWndExtra     = 0;
  26.         wndclass.hInstance     = hInstance;
  27.         wndclass.hIcon     = LoadIcon (NULL,IDI_APPLICATION);
  28.         wndclass.hCursor     = LoadCursor (NULL, IDC_ARROW);
  29.         wndclass.hbrBackground = COLOR_WINDOW + 1;
  30.         wndclass.lpszMenuName  = NULL;
  31.         wndclass.lpszClassName = szAppName;
  32.  
  33.         RegisterClass (&wndclass);
  34.         }
  35.  
  36.        hwnd = CreateWindow (szAppName, szAppName,
  37.                 WS_OVERLAPPEDWINDOW,
  38.                  CW_USEDEFAULT,CW_USEDEFAULT,
  39.                  CW_USEDEFAULT,CW_USEDEFAULT,
  40.                 NULL, NULL, hInstance, NULL) ;
  41.  
  42.        ShowWindow (hwnd, nCmdShow);
  43.        UpdateWindow (hwnd);
  44.  
  45.        while (GetMessage (&msg, NULL, 0, 0))
  46.         {
  47.         TranslateMessage (&msg);
  48.         DispatchMessage (&msg);
  49.         }
  50.        return msg.wParam;
  51.        }
  52.  
  53.  
  54.  
  55.  
  56.   long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  57.  
  58.        {
  59.        static char szBuffer [MAXENV + 1] ;
  60.        static HWND hwndList,hwndText;
  61.        HDC hdc ;
  62.        TEXTMETRIC tm ;
  63.        WORD n ;
  64.  
  65.        switch (message)
  66.         {
  67.         case WM_CREATE :
  68.          hdc = GetDC (hwnd) ;
  69.          GetTextMetrics (hdc , &tm) ;
  70.          ReleaseDC (hwnd , hdc) ;
  71.  
  72.          hwndList = CreateWindow ( "listbox", NULL,
  73.                 WS_CHILD | WS_VISIBLE | LBS_STANDARD,
  74.                 tm.tmAveCharWidth, tm.tmHeight * 3,
  75.                 tm.tmAveCharWidth * 16 +
  76.                        GetSystemMetrics (SM_CXVSCROLL),
  77.                 tm.tmHeight * 5,
  78.                 hwnd,1,
  79.                 GetWindowWord (hwnd, GWW_HINSTANCE), NULL) ;
  80.  
  81.         hwndText = CreateWindow ( "static",NULL,
  82.                     WS_CHILD |  WS_VISIBLE | SS_LEFT,
  83.                     tm.tmAveCharWidth,  tm.tmHeight,
  84.                     tm.tmAveCharWidth * MAXENV, tm.tmHeight,
  85.                     hwnd,2,
  86.                     GetWindowWord (hwnd, GWW_HINSTANCE) , NULL) ;
  87.  
  88.         for (n = 0; environ[n]; n++)
  89.            {
  90.            if (strlen (environ [n]) > MAXENV)
  91.             continue ;
  92.            *strchr (strcpy (szBuffer, environ [n]), '=') = '\0' ;
  93.            SendMessage (hwndList, LB_ADDSTRING, 0,
  94.                 (LONG) (LPSTR) szBuffer ) ;
  95.            }
  96.         return 0 ;
  97.  
  98.  
  99.         case WM_SETFOCUS :
  100.         SetFocus(hwndList) ;
  101.         return 0 ;
  102.  
  103.  
  104.         case WM_COMMAND :
  105.         if (wParam == 1 && HIWORD (lParam) == LBN_SELCHANGE)
  106.            {
  107.            n = (WORD) SendMessage (hwndList, LB_GETCURSEL,0,0L);
  108.  
  109.            n = (WORD) SendMessage (hwndList, LB_GETTEXT,n,(LONG) (LPSTR) szBuffer);
  110.  
  111.            strcpy (szBuffer + n + 1,getenv (szBuffer)) ;
  112.            *(szBuffer + n) = '=';
  113.  
  114.            SetWindowText (hwndText,szBuffer) ;
  115.            }
  116.         return 0;
  117.  
  118.         case WM_DESTROY:
  119.         PostQuitMessage(0);
  120.         return 0;
  121.          }
  122.        return DefWindowProc (hwnd,message,wParam,lParam);
  123.        }
  124.  
  125.